﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Data;
using DevExpress.Web.Mvc;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 被评议人类别定义
    /// </summary>
    /// 
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class PeerReviewBeenVoterClsController : AdminBaseController
    {
        //
        // GET: /Admin/PeerReviewBeenVoterCls/
        /// <summary>
        /// 被评议人类别定义主页
        /// </summary>
        /// <returns>视图</returns>
        /// 
        [AuthorityActionFilter("m_PeerReviewBeenVoterCls", Order = 1)]
        [LogPageActionFilter("被评议人类别定义", Order = 2)]
        public ActionResult Index()
        {
            BLL.PeerReview peerReviewBll = new BLL.PeerReview();

            DataSet ds = peerReviewBll.GetIsFtatus();

            string isFstatus = "";
            if (ds.Tables[0].Rows.Count > 0)
            {
                isFstatus = ds.Tables[0].Rows[0]["FStatus"].ToString();
            }
            else
            {
                isFstatus = "";
            }
            ViewData["isFstatus"] = isFstatus;

            return View();
        }

        /// <summary>
        /// 列表页面
        /// </summary>
        /// <returns>列表页面</returns>
        public ActionResult PeerReviewBeenVoterClsList()
        {
            BLL.PeerReviewResult peerReviewResultBll = new BLL.PeerReviewResult();
            DataSet ds = peerReviewResultBll.GetAllList();
            string visible = "false";
            if (ds.Tables[0].Rows.Count > 0)
            {
                visible = "true";
            }
            ViewData["visible"] = visible;

            int pageIndex = 1;
            List<Model.PeerReviewBeenVoterCls> peerReviewBeenVoterClsList = GetPeerReviewBeenVoterList(pageIndex);

            return View(peerReviewBeenVoterClsList);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageCount">每页显示大小</param>
        /// <param name="pageRowSize">每页显示页数</param>
        /// <returns>集合</returns>
        private List<Model.PeerReviewBeenVoterCls> GetPeerReviewBeenVoterList(int pageIndex, int pageCount = 0, int pageRowSize = 0)
        {

            BLL.Common commonBll = new BLL.Common();
            string fiedls = "*";
            int total;

            DataSet ds = commonBll.GetListByPage("dbo.t_PeerReviewBeenVoterCls", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, "", "FDispSeq", 0, out total);

            BLL.PeerReviewBeenVoterCls peerReviewBeenVoterClsBll = new BLL.PeerReviewBeenVoterCls();


            List<Model.PeerReviewBeenVoterCls> peerReviewBeenVoterClsAllList = new List<Model.PeerReviewBeenVoterCls>();
            List<Model.PeerReviewBeenVoterCls> peerReviewBeenVoterClsList = peerReviewBeenVoterClsBll.DataTableToListVoterCls(ds.Tables[0]);

            pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                peerReviewBeenVoterClsAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (peerReviewBeenVoterClsList.Count > i)
                {
                    peerReviewBeenVoterClsAllList[((pageIndex - 1) * pageRowSize) + i] = peerReviewBeenVoterClsList[i];
                }
            }


            return peerReviewBeenVoterClsAllList;
        }


        /// <summary>
        /// 主要用于刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult PeerReviewBeenVoterClsListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());

            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }


            List<Model.PeerReviewBeenVoterCls> peerReviewBeenVoterClsList = GetPeerReviewBeenVoterList(pageIndex, pageCount, pageRowSize);


            return PartialView("PeerReviewBeenVoterClsListPartial", peerReviewBeenVoterClsList);
        }

        /// <summary>
        /// 加载初始化页面
        /// </summary>
        /// <param name="beenVoterClsCd">被评选人编号</param>
        /// <param name="peerReviewID">主题编号</param>
        /// <returns></returns>
        public ActionResult PeerReviewBeenVoterClsEdit(string beenVoterClsCd, string peerReviewID)
        {
            Model.PeerReviewBeenVoterCls peerReviewBeenVoterClsModel = new Model.PeerReviewBeenVoterCls();

            BLL.PeerReviewBeenVoterCls peerReviewBeenVoterClsBll = new BLL.PeerReviewBeenVoterCls();

           
            if (string.IsNullOrEmpty(beenVoterClsCd) && string.IsNullOrEmpty(peerReviewID))
            {
                ViewData["type"] = "ADD";
                ViewData["ClsCd"] = true;
                ViewData["ReviewID"] = true;
            }
            else 
            {
                peerReviewBeenVoterClsModel = peerReviewBeenVoterClsBll.GetModel(beenVoterClsCd, decimal.Parse(peerReviewID));
                if (peerReviewBeenVoterClsModel == null) 
                {
                    peerReviewBeenVoterClsModel = new Model.PeerReviewBeenVoterCls();
                }
                ViewData["type"] = "Edit";
                ViewData["ClsCd"] = false;
                ViewData["ReviewID"] = false;
            }

            ///主题编号悬着
            ///
            BLL.PeerReview peerReviewBll = new BLL.PeerReview();
            List<Model.PeerReview> peerReviewList = peerReviewBll.GetList();

            List<SelectListItem> itemList = new List<SelectListItem>();
            SelectListItem selectItem;

            foreach (Model.PeerReview model in peerReviewList) 
            {
                selectItem = new SelectListItem();
                selectItem.Text = model.FTitle;
                selectItem.Value = model.FPeerReviewID.ToString();
                ViewData["PeerReviewID"] = model.FPeerReviewID.ToString();
                itemList.Add(selectItem);
            }
            ViewData["List"] = itemList;

            

            return View(peerReviewBeenVoterClsModel);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="peerReviewBeenVoterClsModel">Model对象</param>
        /// <returns></returns>
        public ActionResult Save(Model.PeerReviewBeenVoterCls peerReviewBeenVoterClsModel)
        {
            string type = Fetch.Post("hdtype");
            ViewData["type"] = type;
            
            peerReviewBeenVoterClsModel.FBeenVoterClsCd = Fetch.Post("FBeenVoterClsCd").Trim();
            peerReviewBeenVoterClsModel.FBeenVoterClsName = Fetch.Post("FBeenVoterClsName").Trim();
            peerReviewBeenVoterClsModel.FDispSeq = Int32.Parse(Fetch.Post("FDispSeq").Trim());

            BLL.PeerReviewBeenVoterCls peerReviewBeenVoterClsBll = new BLL.PeerReviewBeenVoterCls();

            try
            {
                if (type == "ADD")
                {
                    peerReviewBeenVoterClsModel.FPeerReviewID = decimal.Parse(ComboBoxExtension.GetValue<string>("FPeerReviewID"));

                    peerReviewBeenVoterClsBll.Add(peerReviewBeenVoterClsModel);

                    ViewData["message"] = "增加操作成功";
                    ViewData["ClsCd"] = true;
                    ViewData["ReviewID"] = true;
                }
                else if (type == "Edit")
                {
                    peerReviewBeenVoterClsModel.FPeerReviewID = decimal.Parse(Fetch.Post("hdPeerReviewID"));

                    peerReviewBeenVoterClsBll.Update(peerReviewBeenVoterClsModel);

                    ViewData["message"] = "修改操作成功";
                    ViewData["ClsCd"] = false;
                    ViewData["ReviewID"] = false;
                }               
            }
            catch(Exception) 
            {
                ViewData["message"] = "操作失败";
            }


            return View("PeerReviewBeenVoterClsEdit", peerReviewBeenVoterClsModel);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="beenVoterClsCd">被评选人类别编号</param>
        /// <param name="peerReviewID">主题编号</param>
        /// <returns>是否删除</returns>
        public string Del(string beenVoterClsCd, string peerReviewID)
        {
            string str = "";
            beenVoterClsCd = Fetch.estimateString(beenVoterClsCd, CheckGetEnum.Safety);
            peerReviewID = Fetch.estimateString(peerReviewID, CheckGetEnum.Safety);
            BLL.PeerReviewBeenVoterCls peerReviewBeenVoterClsBll = new BLL.PeerReviewBeenVoterCls();
            try
            {
                peerReviewBeenVoterClsBll.Delete(beenVoterClsCd, decimal.Parse(peerReviewID));
                str = "ok";
            }
            catch 
            {
                str = "error";
            }

            return str;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public string DelMany(string s)
        {
            string str = "";
            BLL.PeerReviewBeenVoterCls peerReviewBeenVoterClsBll = new BLL.PeerReviewBeenVoterCls();
            string[] array = s.Split(new string[] { "%!^" }, StringSplitOptions.RemoveEmptyEntries);
            try
            {
                foreach (string st in array) 
                {
                    string[] array1 = st.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                    peerReviewBeenVoterClsBll.DelMany(array1[0], decimal.Parse(array1[1]));
                }
                str = "ok";
            }
            catch (Exception)
            {
                str = "error";
            }
            return str;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="beenVoterClsCd">类别编号</param>
        /// <param name="peerReviewID">主题ID</param>
        /// <returns>字符串判断</returns>
        public string Exists(string beenVoterClsCd, string peerReviewID) 
        {
            string str = "";
            BLL.PeerReviewBeenVoterCls peerReviewBeenVoterClsBll = new BLL.PeerReviewBeenVoterCls();
            if (peerReviewBeenVoterClsBll.Exists(beenVoterClsCd, decimal.Parse(peerReviewID)))
            {
                str = "exists";
            }
            else 
            {
                str = "noexists";
            }

            return str;
        
        }

    }
}
